package com.lu.common.service.impl;

import com.lu.common.domain.ConfigurationDO;
import com.lu.common.domain.DbUrl;
import com.lu.common.utils.DBTools;
import com.lu.common.utils.GenUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;


public class GeneratorServiceImpl {

	public List<Map<String, Object>> list(DbUrl dbUrl) {
		DBTools db=new DBTools(dbUrl);
		String sql="select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables"
				+ " where table_schema = (select database())";
		List<Map<String, Object>> list = new ArrayList<>();
		try {
			ResultSet rs=db.getQuery(sql);
			if(rs==null){
				return null;
			}
			while(rs.next()){
                Map<String, Object> map=new HashedMap();
                map.put("tableName", rs.getString(1));
				map.put("engine", rs.getString(2));
				map.put("tableComment", rs.getString(3));
				map.put("createTime", rs.getString(4));
				list.add(map);
            }
		} catch (Exception e) {
			return null;
		}

		return list;
	}

	public byte[] generatorCode(String[] tableNames, DbUrl dbUrl, ConfigurationDO configurationDO) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		DBTools db=new DBTools(dbUrl);
		for(String tableName : tableNames){
			String sql1="select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables \r\n"
					+ "	where table_schema = (select database()) and table_name = '"+tableName+"' ";
			ResultSet rs=db.getQuery(sql1);
			//查询表信息
			Map<String, String> table = new HashMap<>();
			try {
				while(rs.next()){
					table.put("tableName", rs.getString(1));
					table.put("engine", rs.getString(2));
					table.put("tableComment", rs.getString(3));
					table.put("createTime", rs.getString(4));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			String sql2="select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra from information_schema.columns\r\n"
					+ " where table_name = '"+tableName+"' and table_schema = (select database()) order by ordinal_position";
			ResultSet rs2=db.getQuery(sql2);

			//查询列信息
			List<Map<String, String>> columns = new ArrayList<>();
			try {
				while(rs2.next()){
					Map<String, String> map=new HashMap();
					map.put("columnName", rs2.getString(1));
					map.put("dataType", rs2.getString(2));
					map.put("columnComment", rs2.getString(3));
					map.put("columnKey", rs2.getString(4));
					map.put("extra", rs2.getString(5));
					columns.add(map);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			//生成代码
			GenUtils.generatorCode(table, columns, zip,configurationDO);
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

}
